<?php 
/*
 * 开始处理,就这一个文件就可以了
 */
/*
$array = get_file_doc_content_array("data@fajia_10-11@28_6.doc");
print_r($array);
exit;
*/
write_log("开始");
//open-flash-chart不支持两个参数,所以就用了一个参数传过来的值
$temp = isset($_GET['file'])?$_GET['file']:null;
if(empty($temp))
	exit("参数不能为空");
$temp = explode("@@", $temp);
$file = isset($temp[0])?$temp[0]:null;
$step = isset($temp[1])?$temp[1]:null;
write_log("file:$file,step:$step");

$array = get_file_doc_content_array($file);
if(!isset($array[$step]))
	exit("没有这么多");

include_once( "js" .DIRECTORY_SEPARATOR . 'open-flash-chart'.DIRECTORY_SEPARATOR.'open-flash-chart.php' );
$zhuchang = array();
$visit = array();
$line = array();
//最大排名
$max_rank = 0;

$no_rank = 0;
$data = array(
	"value"=>
		//每一个参数是排名,没有则用$no_rank,每二个是主球队的得分,每三个是它球队的得分
		$array[$step]
);
write_log(("数据:".json_encode($data)));
$g = new graph();
//$g->title( '德甲_2011-10-22_上半部分', '{font-size: 23px;}' );
/*
//画一个点,x,y,大小
$zhuchang[] = new point(3,-15,5);
$visit[] = new point(3,-18,5);
输出一个折线图出来
$line = array(0,0,-8,-17,-6,-7,-9,-14,-4,-13,-16,-0,-13,-18,-7,-5,-4,-16,-3,-10,-1);
*/
array_push($line, 0);
foreach ($data["value"] as $key => $value) {
	$max_rank = ($max_rank>$value[0])?$max_rank:$value[0];
	array_push($line, -$value[0]);
	$zhuchang[] = new point(($key+1),(-$value[0]+$value[1]),5);
	$visit[] = new point(($key+1),(-$value[0]-$value[2]),5);
}

$g->scatter( $zhuchang, 3, '#ff0000', '主场', 12 );
$g->scatter( $visit, 3, '#9933CC', '客场', 12 );
$g->set_data( $line ); 

$g->line_hollow( 2, 4, '0x80a033', 'Line', 10 );

//鼠标放上的
$g->set_tool_tip( 'x:#x_label#<br>y:#val#' );

$g->set_x_offset( false );

$max_rank = 22;

$g->set_y_label_style( 10, '#9933CC' );
$g->y_label_steps($max_rank+6);
$g->set_x_label_style( 10, '#9933CC' );
//$g->set_x_legend( '下面的介绍' );

$g->set_y_min(-($max_rank+5));
$g->set_y_max(1);
$g->set_x_min(0);
$g->set_x_max($key+2);

// display the data
echo $g->render();

/**
 * 将word文件按照顺序组织成数组返回数据
 * 正常用时:0.008秒
 * 
 * 传入参数data@fajia_10-11@10_3.doc
 * 将#号换成/,取得对应的文件内容,来处理里面的数据
 * 
 * @param string $address
 */
function get_file_doc_content_array($address){
	$temp_address = str_replace("@", "/", $address);
	if(!is_file($temp_address))
		return null;
	$body = file_get_contents($temp_address);
	//去除第一行中的table
	$body = preg_replace('/<table width=100%(.+?)<\/table>/s', "", $body);
	//取得每一个行
	preg_match_all('/<tr>(.+?)<\/tr>/s',$body,$arr);
	if(empty($arr[1]))
		return;
	$data = array();
	foreach ($arr[1] as $value) {
		preg_match_all('/<td(.+?)<\/td>/s',$value,$td);
		$bifen = strip_tags($td[0][2]);
		$temp = explode("-", $bifen);
		$bifen_left = trim($temp[0]);
		$bifen_right = trim($temp[1]);
		$bifen_left = is_numeric($bifen_left)?$bifen_left:0;
		$bifen_right = is_numeric($bifen_right)?$bifen_right:0;
		//根据排名所列得到排名
		if(strpos($td[0][1], "strong") !== false){
			////每一个参数是排名,没有则用$no_rank,每二个是主球队的得分,每三个是它球队的得分
			$data[] = array(handler_paiming($td[0][1]), $bifen_left, $bifen_right);
		}else{
			$data[] = array(handler_paiming($td[0][3]), $bifen_right, $bifen_left);
		}
	}
	unset($arr);
	if(empty($data))
		return null;
	/**
	 * 将60行比赛分10行放入一个单位内
	 */
	$okay = array();
	$i = 0;
	$lun = 0;
	foreach ($data as $value) {
		if($i % 10 != 0){
			$okay[$lun][] = $value;
		}else{
			$lun++;
			$okay[$lun][] = $value;
		}
		$i++;
	}
	unset($data);
	
	$news_data = array();
	//数已经还在的数据反序
	foreach ($okay as $key => $value) {
		$news_data[$key] = array_reverse($value);
	}
	unset($okay);

	return $news_data;
}

/**
 * 处理第二列或第四列中的排名,没有排则,则返回0
 * @param string $td 第二(四)列的内容
 */
function handler_paiming($td){
	if(empty($td))
		return 0;
	$temp = strip_tags($td);
	preg_match('/\((.+)\)/s',$temp,$temp_paiming);
	if(empty($temp_paiming[1]))
		$paiming = 0;
	else 
		$paiming = $temp_paiming[1];
	return $paiming;
}

/**
 * 写日志
 * 
 * 时间是产生的微秒时间
 * 
 * @param string $v 日志内容
 * @param string $level 等级
 */
function write_log($v="", $level="INFO"){
	$fp = fopen("log.".date("Y-m-d").".txt", "a");
	if(!$fp){
		return false;
	}else{
		$temp_time = microtime(true);
		$v = date("Y-m-d H:i:s").".".substr(($temp_time-floor($temp_time)), 2, 6). " " . $level . ":" . $v . "[url:" . $_SERVER['REQUEST_URI'] . ",ip:" . getip() . "]";
		
		fwrite($fp, $v."\r\n");
		fclose($fp);
		return true;
	}
}

global $_client_real_ip;
/**
 * 得到客户端ip
 */
function getip(){
	global $_client_real_ip;
	if(null === $_client_real_ip){
		if (isset($_SERVER)){
			if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
				$_client_real_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
			}elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
				$_client_real_ip = $_SERVER['HTTP_CLIENT_IP'];
			}else{
				$_client_real_ip = $_SERVER['REMOTE_ADDR'];
			}
		}else{
			if(getenv("HTTP_X_FORWARDED_FOR")){
				$_client_real_ip = getenv( "HTTP_X_FORWARDED_FOR");
			}elseif(getenv("HTTP_CLIENT_IP")){
				$_client_real_ip = getenv("HTTP_CLIENT_IP");
			}else{
				$_client_real_ip = getenv("REMOTE_ADDR");
			}
		}
		return (preg_match('/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/',$_client_real_ip))?$_client_real_ip:0;
	}else{
		return $_client_real_ip;
	}
}